package impl;

import bean.Food;
import bean.User;
import dao.UserDao;
import util.DBUtils;

import javax.jws.soap.SOAPBinding;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends DBUtils implements UserDao {
    @Override
    public User getUserName(String user) {
        User us = new User();
        try {
            String sql = "select password from user where user=?";
            List list = new ArrayList();
            list.add(user);
            ResultSet rs = query(sql,list);
            while (rs.next()){
                us.setUserpwd(rs.getString("password"));
            }

        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll();
        }
        return us;
    }

    @Override
    public int setUserName(String username, String userpwd) {

        String sql = "insert into user(user,password) values(?,?)";
        List list = new ArrayList();
        list.add(username);
        list.add(userpwd);
        int h = updata(sql,list);

        return h;
    }

    @Override
    public List<Food> getFood() {
        Food food = new Food();
        List<Food> list = new ArrayList<>();
        String sql = "select * from food ";
        ResultSet rs = queryd(sql);
        try{
            while(rs.next()){
                food.setFoodname(rs.getString("foodname"));
                food.setPrice(rs.getInt("price"));
                food.setAddress(rs.getString("address"));
                food.setData(rs.getString("data"));
                list.add(food);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public int setFood(Food food) {
        int h = 0;
        String sql = "insert into food values(?,?,?,?)";
        List list = new ArrayList();
        list.add(food.getFoodname());
        list.add(food.getPrice());
        list.add(food.getAddress());
        list.add(food.getData());
        h = updata(sql,list);
        return h;
    }

}
